<?php
/**
* My Handy Restaurant
*
* http://www.myhandyrestaurant.org
*
* My Handy Restaurant is a restaurant complete management tool.
* Visit {@link http://www.myhandyrestaurant.org} for more info.
* Copyright (C) 2003-2005 Fabio De Pascale
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* @author		Fabio 'Kilyerd' De Pascale <public@fabiolinux.com>
* @package		MyHandyRestaurant
* @copyright		Copyright 2003-2005, Fabio De Pascale
*/

function install_check_config_writeable($name) {
	if(!file_exists($name)) {
		$dirname=dirname($name);
		if(!is_writeable($dirname))
			return 2;
	} elseif (!is_writeable($name))
		return 1;
}

function install_get_config_data($name) {
	if(file_exists($name)) {
		include("./conf/config.inc.php");
		$data['server']=$cfgserver;
		$data['username']=$cfguser;
		$data['password']=$cfgpassword;
		$data['db_common']=$db_common;
		$data['tableprefix']=$table_prefix;
		return $data;
	}
	$data['server']='localhost';
	$data['username']='root';
	$data['password']='';
	$data['db_common']='handyrestaurant_common';
	$data['tableprefix']='';
	return $data;

}

function install_config_data_form() {
	$name='./conf/config.inc.php';
	$data= install_get_config_data($name);

	switch ($conf_writable=check_conf_file($name)) {
		case 1:
			echo 'Attention:<br/>
You must set write permission to the conf/config.inc.php file<br/>
before submitting this form.<br/>
<br />'."\n";
			break;
		case 2:
			echo 'Attention:<br/>
The configuration file (conf/config.inc.php) has not been found, therefore it will be created.<br/>
You must set write permission to the conf/ directory<br/>
before submitting this form.<br/>
<br />'."\n";
			break;
	}

?>
<form action="install.php" method="post">
<input type="hidden" name="command" value="write_config">
<table>
	<tr>
		<td>MySQL username:</td>
		<td><input type="text" name="username" value="<?php echo $data['username']; ?>"></td>
	</tr>
	<tr>
		<td>MySQL password:</td>
		<td><input type="password" name="password" value="<?php echo $data['password']; ?>"></td>
	</tr>
	<tr>
		<td>MySQL server name or ip address:</td>
		<td><input type="text" name="server" value="<?php echo $data['server']; ?>"></td>
	</tr>
	<tr>
		<td>Main database (usually called common):</td>
		<td><input type="text" name="db_common" size="30" value="<?php echo $data['db_common']; ?>"></td>
	</tr>
	<tr>
		<td colspan="2"><center><input type="submit" value="Submit"></center></td>
	</tr>
</table>

</form>

<a href="./install.php">Go to main installation page</a>


<?php
}

function install_write_config_file($data) {
	if(!is_array($data))
		return 3;

	$name='./conf/config.inc.php';
	if($res_loc=install_check_config_writeable($name)) {
			$error_msg = common_header('Output files not writeable',' class="mgmt_body"');

			switch($res_loc) {
				case 1: $err='config file not writeable'; break;
				case 2: $err='config dir not writeable'; break;
			}

			$error_msg .= GLOBALMSG_CONFIG_FILE_NOT_WRITEABLE.'<br><br>(err '.$res_loc.': '.$err.')<br>'."\n";
			$error_msg .= common_bottom();
			echo $error_msg;
			return 1;
	}
	unset($res_loc);

	$config = '<?php
/*
--------------------------------------------------------------
My Handy Restaurant Configuration File
--------------------------------------------------------------
This file has been automatically generated by My Handy Restaurant

Edit it by hand only if you know what you\'re doing.
Otherwise use the install.php page.
---------------------------------------------------------------
*/

$cfgserver=\''.$data['server'].'\';
$cfguser=\''.$data['username'].'\';
$cfgpassword=\''.$data['password'].'\';

$db_common=\''.$data['db_common'].'\';

$table_prefix=\''.$data['tableprefix'].'\';

?>';


	$fp = fopen ($name, 'wt');
	if (!fwrite($fp, $config)) {
		echo "Cannot write to file ($name)";
		return 2;
	}
	fclose($fp);
	return 0;
}

function install_list_options(){
?>
	<h3>Smart Restaurant - Isntallation.</h3>
	</center>
	1. <a href="install.php?command=reconfigure">Write basic configuration file</a> of the software.<br>
	2.a. <a href="install.php?command=fresh_install">Fresh install</a> the databases.<br>
	<b>or</b><br>
	2.b. <a href="./admin/upgrade.php?command=none">Upgrade</a> the databases (You must have an installed system with version number greater than 0.8.3).<br>

	3. <a href="conf/index.php">Configure (or Reconfigure) the system</a> (user <b>Administrator</b>, password <b>password</b>).<br>
	<hr>
	<center>
<?php
}

function install_create_db(){
	require("conf/config.inc.php");

	//$link = mysql_pconnect ($cfgserver, $cfguser,$cfgpassword) or die ('GLOBALMSG_DB_CONNECTION_ERROR');
	$link = mysql_pconnect ($cfgserver, $cfguser,$cfgpassword);
	if(!$link) return 2;

	if(!empty($db_common)) {
		echo 'Database '.$db_common.' creation';

		$query='CREATE DATABASE IF NOT EXISTS `'.$db_common.'`';
		mysql_query ($query);
		if($errno=mysql_errno()) {
			$msg="Error in ".__FUNCTION__." - file ".__FILE__.' line '.__LINE__;
			$msg.='mysql: '.mysql_errno().' '.mysql_error()."\n";
			$msg.='query: '.$query."\n";
			//error_msg(__FILE__,__LINE__,$msg);
			echo nl2br($msg)."\n";
			return $errno;
		} else {
			echo '.'."<br>\n";
		}
	}

	return 0;
}

function install_fill_tables(){
	require("./conf/config.inc.php");
	require("./conf/config.constants.inc.php");
	
	require("./include/mysql_class.php");

	global $output;
	
	$file[0]='./'.$location['common']['complete'];
	$file[1]='./'.$location['account']['complete'];

	$sql_file=$file[0];
	$verbosity=0;
	
	$_SESSION['common_db']=$db_common;
	
	$dbman = new db_manager ($cfgserver, $cfguser, $cfgpassword);
	if($err) return $err;
	
	$dbman -> select_db($_SESSION['common_db']);
	
	$err=$dbman->upgrade_from_file($sql_file,$verbosity,false);
	if($err) return $err;
	
	echo nl2br($output);
	
	return 0;
}

?>
